package com.gym.openapi.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gym.openapi.common.ResponseResult;
import com.gym.openapi.common.ResultTable;
import com.gym.openapi.dao.ApiMappingMapper;
import com.gym.openapi.entity.ApiMapping;
import com.gym.openapi.services.ApiMappingServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ming
 * 2021/11/26
 * description:
 */
@Service
public class ApiMappingServicesImpl implements ApiMappingServices {

    @Autowired
    private ApiMappingMapper apiMappingMapper;


    @Override
    public ResultTable apiTable(Integer page, Integer limit, String gatewayApiName, Byte state) {
        PageHelper.startPage(page, limit);
        List<ApiMapping> apiMappingMapperList = apiMappingMapper.apiTable(gatewayApiName, state);
        PageInfo<ApiMapping> apiMappingPageInfo = new PageInfo(apiMappingMapperList);
        return new ResultTable(0,"msg", (int) apiMappingPageInfo.getTotal(),apiMappingPageInfo.getList());
    }

    @Override
    public ResponseResult del(Integer[] ids) {
        int i = apiMappingMapper.del(ids);
        if (i>0) {
            return ResponseResult.getSuccess("删除api成功");
        }
        return ResponseResult.getFail("删除api失败");
            }

    @Override
    public ResponseResult update(ApiMapping apiMapping1) {
        if (apiMapping1!=null) {
            int i = apiMappingMapper.updateByPrimaryKeySelective(apiMapping1);
            if (i>0) {
                return ResponseResult.getSuccess("api改好了");
            }
        }
        return ResponseResult.getFail("api修改没好");
    }

    @Override
    public ResponseResult add(ApiMapping apiMapping2) {
        if (apiMapping2!=null) {
            int i = apiMappingMapper.insertSelective(apiMapping2);
            if (i>0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("添加不成功");
    }
}
